package com.tuzhi.filestore.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;


@Configuration
@EnableSwagger2
@Profile({"dev","uat"})
public class SwaggerConfig {


    @Bean
    public Docket docketAll(Environment environment) {
         return new Docket(DocumentationType.SWAGGER_2).groupName("all");
       // return buildDocket(environment,"all","*.controller");
    }

    @Bean
    public Docket docketSys(Environment environment) {
        return buildDocket(environment,"sys","com.tuzhi.filestore.controller");
    }






    private Docket buildDocket(Environment environment,String groupName,String basePackage){
        //设置要显示的Swagger环境
        Profiles profiles = Profiles.of("dev", "test");
        //通过environment.acceptsProfiles判断是否c处在自己设定的环境当中
        boolean flag = environment.acceptsProfiles(profiles);
        ApiSelectorBuilder apiSelectorBuilder = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName(groupName)
                .enable(flag)//enable是否启用swagger，如果为false则swagger不能在浏览器中访问
                .select().apis(RequestHandlerSelectors.basePackage(basePackage));

         return apiSelectorBuilder.build();
    }

    //配置Swagger信息的apiInfo
    private ApiInfo apiInfo() {
        //作者信息
       // Contact contact = new Contact("lu", "codekitty.cn:8000", "L18249290950@126.com");

        return new ApiInfo(
                "图能智造API文档",
                "",
                "1.0",
                "",
                null,
                "",
                "",
                new ArrayList()
        );
    }
}
